package com.tedu.openmedical.medicalsystem.fqa.service.impl;

import com.tedu.openmedical.medicalsystem.fqa.dto.FAQQuery;
import com.tedu.openmedical.medicalsystem.fqa.dto.FAQSaveParam;
import com.tedu.openmedical.medicalsystem.fqa.entity.FAQ;
import com.tedu.openmedical.medicalsystem.fqa.service.FAQService;
import com.tedu.openmedical.medicalsystem.fqa.vo.FAQVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tedu.openmedical.medicalsystem.fqa.mapper.FAQMapper;

import java.util.Date;
import java.util.List;

@Service
public class FAQServiceImpl implements FAQService {

    @Autowired
    private FAQMapper FAQMapper;

    @Override
    public List<FAQVO> select(FAQQuery query) {
        if (query.getTimes() == null) {
            return FAQMapper.select(query);
        }
        query.setStartTime(query.getTimes()[0]);
        query.setEndTime(query.getTimes()[1]);
        return FAQMapper.select(query);
    }

    @Override
    public void save(FAQSaveParam faqSaveParam) {
        FAQ faq = new FAQ();
        BeanUtils.copyProperties(faqSaveParam, faq);
        if (faqSaveParam.getId() == null){
            faq.setUpdateTime(new Date());
            faq.setCreateTime(new Date());
            FAQMapper.insert(faq);
        } else {
            faq.setUpdateTime(new Date());
            faq.setUpdateBy(faqSaveParam.getCreateBy());
            FAQMapper.update(faq);
        }
    }

    @Override
    public void delete(Long id) {
        FAQMapper.delete(id);
    }
}
